﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Indigo2.Web;
using System.ServiceModel.DomainServices.Client;

namespace Indigo2.Views
{
    public partial class TaskType : Page
    {
        

        TaskTypeDomainContext bah = new TaskTypeDomainContext();


        public TaskType()
        {
            InitializeComponent();
            LoadOperation<tasktype> loadops = bah.Load(bah.GetTasktypesQuery());

            loadops.Completed += (s, a) =>
                {
                    DGTaskType.ItemsSource = bah.tasktypes;
                };

            

           
 
          
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void tasktypeDomainDataSource_LoadedData_1(object sender, System.Windows.Controls.LoadedDataEventArgs e)
        {

            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
           
            tasktype newtasktype = new tasktype();

            

               newtasktype.TaskTypeCode = txtTaskCode.Text;
                newtasktype.TaskTypeDesc= txtTaskDescription.Text;
                newtasktype.TaskTypeDescLong = txtLongDesc.Text;
                newtasktype.UserLog = txtUserID.Text;
                newtasktype.UserTimeLog = DateTime.Today;

                bah.tasktypes.Add(newtasktype);

                

           
            
            /* VB Style Does not work at all 
            txtIPaddress.Text = "";
            txtLongDesc.Text = "";
            txtTaskCode.Text = "";
            txtTaskDescription.Text = "";
            txtTaskTypeID.Text = "";
            txtUserID.Text = "";
            txtUserTimeLog.Text = "";

            btnNewTaskType.IsEnabled = false;
            */
           // tasktype tt = new tasktype();
               
           

            //EntityChangeSet changeSet = bah.EntityContainer.GetChanges();

            /* NOTES
            EntityQuery<tasktype> query =
                from c in bah.GetTasktypesQuery()
                where c.TaskTypeDesc.Contains("bah")
                select c;*/

            

        }
        public void cleartextbox()
        {
            txtTaskCode.Text = "";
            txtTaskDescription.Text = "";
            txtLongDesc.Text = "";
            txtTaskTypeID.Text = "";
        }

        private void BtnSave_Click(object sender, RoutedEventArgs e)
        {
            bah.SubmitChanges();
            cleartextbox();

        }

        private void btnRemove_Click(object sender, RoutedEventArgs e)
        {
            tasktype taskdelete = this.DGTaskType.SelectedItem as tasktype;
            this.bah.tasktypes.Remove(taskdelete);


                    

            //var TaskTypeIDNumber = int.Parse(txtTaskTypeID.Text);

            //var result = bah.tasktypes.Where(taskID => taskID.TaskTypeId == TaskTypeIDNumber);
            
           //bah.Load(result, DataDelete, null);

            //tasktype tasktypetodelete = DGTaskType.SelectedItem as tasktype;
            //bah.tasktypes.Remove(result);

                
            
        }




        /*
        private void DataDelete(LoadOperation<tasktype> lo)
        {
            tasktype tt = lo.Entities.First();

            bah.tasktypes.Remove(tt);

            try
            {
                bah.SubmitChanges();
                MessageBox.Show("Data Deleted");
            }
            catch (Exception except)
            {
                MessageBox.Show("failed due to " + except.Message);

            }
         */
        //}

    }
}
